//
//  MQEmoticonToolBar.swift
//  SinaWeiBo
//
//  Created by 毛强 on 16/2/3.
//  Copyright © 2016年 itheima. All rights reserved.
//

import UIKit
/**
 Description：表情类型
 
 - Recent:  <#Recent description#>
 - Default: <#Default description#>
 - Emoji:   <#Emoji description#>
 - Lxh:     <#Lxh description#>
 */
enum MQEmoticonType:Int{
    case Recent  = 1000
    case Default = 1001
    case Emoji   = 1002
    case Lxh     = 1003
    
}

class MQEmoticonToolBar: UIStackView {
    
    var section:Int?{
        didSet{
            
            //为什么我点击btn，记录的是上一个tag？
            let btn = viewWithTag(section! + 1000) as! UIButton
            if btn != currentBtnSelected{
                return
            }
            currentBtnSelected?.selected = false
            btn.selected = true
            currentBtnSelected = btn
            printfLog(section)
        }
    }
    var currentBtnSelected:UIButton?
    
    var didSelectToolBarBtnClosure: ((emoticonType:MQEmoticonType)->())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
        
    }
    private func setupUI(){
        distribution = .FillEqually
        addChildBtn("最近",bgImageName: "compose_emotion_table_left",emoticonType:.Recent)
        addChildBtn("默认", bgImageName: "compose_emotion_table_mid",emoticonType:.Default)
        addChildBtn("Emoji", bgImageName: "compose_emotion_table_mid",emoticonType:.Emoji)
        addChildBtn("浪小花", bgImageName: "compose_emotion_table_right",emoticonType:.Lxh)

    }
    
    private func addChildBtn(title:String, bgImageName:String,emoticonType:MQEmoticonType){
        let btn = UIButton()
        btn.addTarget(self, action: "didSelectChildBtn:", forControlEvents: UIControlEvents.TouchUpInside)
        btn.tag = emoticonType.rawValue
        btn.setTitle(title, forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.grayColor(), forState: UIControlState.Selected)
        
        btn.setBackgroundImage(UIImage(named: "\(bgImageName)_normal"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "\(bgImageName)_selected"), forState: UIControlState.Selected)
        self.addArrangedSubview(btn)
    }
    
    @objc private func didSelectChildBtn(btn:UIButton){
        if btn == currentBtnSelected{
            return
        }
        currentBtnSelected?.selected = false
        btn.selected = true
        currentBtnSelected = btn
        
        
        didSelectToolBarBtnClosure?(emoticonType: MQEmoticonType(rawValue: btn.tag)!)
    }
    
    func choiceBtn(section:Int){
        let btn = viewWithTag(section + 1000) as! UIButton
        if btn == currentBtnSelected{
            return
        }
        currentBtnSelected?.selected = false
        btn.selected = true
        currentBtnSelected = btn
    }
    
}
